
package com.bdtop.dbl.ui.view;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.bdtop.dbl.R;

/**
 * @ClassName: ConfirmDialog
 * @Description: TODO
 * @author: liyongqiang
 * @date:2014-10-17
 */
public class ConfirmDialog {

    private Dialog mDialog;

    public ConfirmDialog(Context context, int msgResId, int confirmResId, int cancelResId,
            final DialogInterfaceListener listener, boolean shadow) {
        this(context, context.getString(msgResId), context.getString(confirmResId), context
                .getString(cancelResId), listener, shadow);
    }

    public ConfirmDialog(Context context, int resId, final DialogInterfaceListener listener,
            boolean shadow) {
        this(context, context.getString(resId), listener, shadow);
    }

    public ConfirmDialog(Context context, String message, final DialogInterfaceListener listener,
            boolean shadow) {
        mDialog = new Dialog(context,
                shadow ? R.style.Theme_Light_NoTitle_Dialog
                        : R.style.Theme_Light_NoTitle_NoShadow_Dialog);
        View content = LayoutInflater.from(context).inflate(
                R.layout.dialog_confirm_2, null);
        mDialog.setContentView(content);
        ((TextView) mDialog.findViewById(R.id.tx_message)).setText(message);
        ((Button) mDialog.findViewById(R.id.btn_confirm)).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View view) {
                listener.confirmListener(mDialog);
            }
        });

        ((Button) mDialog.findViewById(R.id.btn_cancel)).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View view) {
                listener.cancelListener(mDialog);
            }
        });
    }

    public ConfirmDialog(Context context, String message, String confirmText, String cancelText,
            DialogInterfaceListener listener, boolean shadow) {
        this(context, message, listener, shadow);
        ((Button) mDialog.findViewById(R.id.btn_confirm)).setText(confirmText);
        ((Button) mDialog.findViewById(R.id.btn_cancel)).setText(cancelText);
    }

    public interface DialogInterfaceListener {
        void confirmListener(Dialog mDialog);

        void cancelListener(Dialog mDialog);
    }

    public void show(boolean canCancel) {

        mDialog.setCanceledOnTouchOutside(canCancel);
        mDialog.setCancelable(canCancel);
        mDialog.show();
    }

    public ConfirmDialog(Context context, String message,
            boolean shadow, String cencelText) {
        mDialog = new Dialog(context,
                shadow ? R.style.Theme_Light_NoTitle_Dialog
                        : R.style.Theme_Light_NoTitle_NoShadow_Dialog);
        View content = LayoutInflater.from(context).inflate(
                R.layout.dialog_confirm_2, null);
        mDialog.setContentView(content);
        ((TextView) mDialog.findViewById(R.id.tx_message)).setText(message);
        mDialog.findViewById(R.id.btn_confirm).setVisibility(View.GONE);
        mDialog.findViewById(R.id.btn_divider).setVisibility(View.GONE);
        
        if (cencelText != null) {
            ((Button) mDialog.findViewById(R.id.btn_cancel)).setText(cencelText);
        }
        mDialog.findViewById(R.id.btn_cancel).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                mDialog.dismiss();
            }
        });
    }

    public boolean isShowing() {
        if (mDialog == null)
            return false;
        else
            return mDialog.isShowing();
    }
}
